package com.sinaapp.mjfinal.mvc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.abel533.echarts.data.WordCloudData;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.WordCloud;
import com.github.abel533.echarts.style.ItemStyle;
import com.github.abel533.echarts.style.itemstyle.Normal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sinaapp.mjfinal.mvc.base.BaseService;
import com.sinaapp.mjfinal.mvc.model.TmallMobile;
/**
 * 天猫手机销量业务
 * @author binchao
 * 2016年2月28日
 */
public class TmallMobileService extends BaseService {
	
	public static final TmallMobileService service = new TmallMobileService();
	
	
	/**
	 * 词云
	 * @return
	 */
	public String wordCloud() {
		List<Record> counts = Db.find(getSql("mjfinal.tmallmobile.wordcount"));
		List<WordCloudData> cloudDatas = new ArrayList<>();
		for(Record r : counts) {
			WordCloudData w = new WordCloudData(r.getStr("品牌"), r.getBigDecimal("count").intValue())
					.itemStyle(new ItemStyle().normal(createRandomItemStyle()));
			cloudDatas.add(w);
		}
		GsonOption option = new GsonOption();
		option.title().text("天猫手机销量数据");
        option.tooltip().show(true).formatter("{a} <br/>{b} : {c} 台");
        WordCloud wordCloud = new WordCloud("手机销量");
        wordCloud.size("80%", "80%");
        wordCloud.textRotation(0, 45, 90, -45);
        wordCloud.textPadding(0);
        wordCloud.autoSize().enable(true).minSize(14);
        wordCloud.data(cloudDatas.toArray());
        
        option.series(wordCloud);
		return option.toString();
	}
	
	/**
	 * 手机销售前十
	 * @return
	 */
	public List<Record> top10() {
		String sql = getSql("mjfinal.tmallmobile.top10");
		return Db.find(sql);
	}

	/**
	 * 生成随机色
	 * @return
	 */
	private Normal createRandomItemStyle() {
        Normal normal = new Normal();
        normal.color("rgb(" + Math.round(Math.random() * 160) + ","
                + Math.round(Math.random() * 160) + ","
                + Math.round(Math.random() * 160) + ")");
        return normal;
    }
}
